package com.ydd.controller.user;


import com.ydd.dto.CateAddDTO;
import com.ydd.dto.CateListDTO;
import com.ydd.entity.Cate;
import com.ydd.result.Result;
import com.ydd.service.ICateService;
import com.ydd.vo.CateListVO;
import com.ydd.vo.CateRowInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Slf4j
@Api(tags = "内容管理")
@RestController
@RequestMapping("/admin/ydd_cate")
public class CateController {
    @Autowired
    private ICateService iCateService;

    /**
     * 分类详情
     *
     * @param id
     * @return
     */
    @ApiOperation("分类详情,根据id查询分类")
    @GetMapping("/rowInfo")
    public Result rowInfo(Integer id) {
        log.info("分类id：{}", id);
        Cate byId = iCateService.getById(id);
        if (byId==null){
            return Result.error("id不存在");
        }
        CateRowInfoVO cateRowInfoVO = new CateRowInfoVO();
        BeanUtils.copyProperties(byId, cateRowInfoVO);
        return Result.success(cateRowInfoVO);
    }

    /**
     * 分页分类列表
     *
     * @param cateListDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分类列表查询")
    public Result list(CateListDTO cateListDTO) {
        CateListVO cateListVO = iCateService.Page(cateListDTO);
        log.info("查询：{}",cateListDTO);
        return Result.success(cateListVO);
    }

    @ApiOperation("分类删除，根据id删除")
    @GetMapping("/del")
    public Result del(Integer id) {
        iCateService.removeById(id);
        return Result.success();
    }

    /**
     * 分类修改
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改分类")
    public Result update(@RequestBody CateRowInfoVO cateRowInfoVO) {
        log.info("修改分类：{}", cateRowInfoVO);
        Cate cate = new Cate();
        BeanUtils.copyProperties(cateRowInfoVO, cate);
        iCateService.updateById(cate);
        return Result.success();
    }

    /**
     * 分类添加
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("分类添加")
    public Result add(@RequestBody CateAddDTO cateAddDTO) {
        log.info("分类添加：{}", cateAddDTO);
        Cate cate = new Cate();
        BeanUtils.copyProperties(cateAddDTO, cate);
        iCateService.save(cate);
        return Result.success();
    }

    /**
     * 分类查询全部
     *
     * @return
     */
    @GetMapping("/all")
    @ApiOperation("分类列表全部查询")
    public Result all() {
        List<CateRowInfoVO> list = iCateService.all();
        return Result.success(list);
    }

}
